<template>
	<view class="ring_msg">
		<view class="title">
			<uni-icons class="left" color="black" size="28" type="left" @tap="goBack"></uni-icons>
			<!-- 站位 -->
			<view>设备设置</view>
			<view class="right"> </view>
		</view>

		<view class="card">
			<image class="ring" src="@/static/ring/ring2.png" mode="heightFix" />
			<view class="info">
				<view class="name">
					<text class="ring_name">{{ ringName }}</text>
					<text v-if="connected" class="status link">已连接</text>
				</view>
				<view class="battery">
					<view class="progress" :style="{ width: batteryLevel + '%' }"></view>
					<view class="value">{{ batteryLevel }}%</view>
				</view>

				<view class="addr_name">戒指MAC地址： </view>
				<view class="addr_mac"> {{ ringMac }} </view>
			</view>
		</view>

		<view class="item">
			<h3>设备管理</h3>
			<view class="card" @click="showUnbindRing">
				<view class="label">解绑戒指</view>
				<uni-icons color="#999999" size="16" type="right"></uni-icons>
			</view>
			<view class="tips">这一操作会清空现在戒指中存储的数据。</view>

			<view class="card">
				<view class="label">固件更新（当前版本:{{ version.softwareVersion }}）</view>
				<uni-icons color="#999999" size="16" type="right"></uni-icons>
			</view>
			<view class="tips">将您的戒指放入充电盒内，进行固件更新。</view>
		</view>

		<view class="item">
			<h3>使用指南</h3>
			<view class="card">
				<view class="label">使用指南</view>
				<uni-icons color="#999999" size="16" type="right"></uni-icons>
			</view>
			<view class="tips">了解戒指的功能。</view>

			<view class="card" @click="toService">
				<view class="label">联系客服</view>
				<uni-icons color="#999999" size="16" type="right"></uni-icons>
			</view>

			<view class="card" @click="toShareLog">
				<view class="label">分享日志</view>
				<uni-icons color="#999999" size="16" type="right"></uni-icons>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		unbindUserRing
	} from "@/api/ring/bind.js";
	import {
		BLUETOOTH_STATUS
	} from "@/store/BluetoothManager.js";
	import store from "@/store/index.js";
	import SqLiteAnaData from "@/js_sdk/sqLite/analysis.js";

	const FileShare = uni.requireNativePlugin("life-FileShare");

	export default {
		data() {
			return {
				isCharging: false,
				batteryLevel: 0,
			};
		},
		computed: {
			ringName() {
				const {
					name,
					mac,
					customName
				} = store.state.ringDataManage.ring;
				return customName || name || mac;
			},

			ringMac() {
				const {
					mac
				} = this.$store.state.ringDataManage.ring;
				return mac;
			},

			battery() {
				return this.$store.state.ringDataManage.battery;
			},

			version() {
				return this.$store.state.ringDataManage.version;
			},
			connected() {
				return store.state.bleManage.bluetoothStatus === BLUETOOTH_STATUS.CONNECTED;
			},
		},
		watch: {
			battery: {
				handler({
					batteryLevel,
					batteryCharging
				}) {
					this.isCharging = batteryCharging;
					this.batteryLevel = batteryLevel;
				},
				deep: true,
				immediate: true,
			},
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},

			toShareLog() {
				try {
					this.shareWithCopyFile2PDF();
					// if (uni.getSystemInfoSync().platform === "android") {
					// 	this.exportWithAndroid();
					// } else {
					// 	// /var/mobile/Containers/Data/Application/E0707789-4FE9-4089-8F73-084AF1B81BA6/Library/Caches/Pandora/downloads/data.db
					// 	this.exportWithIOS();
					// }
				} catch (error) {
					console.log("分享失败", error);
				}
			},

			async exportWithAndroid() {
				new Promise((resolve, reject) => {
						plus.io.requestFileSystem(
							plus.io.PUBLIC_DOWNLOADS,
							function(fs) {
								fs.root.getFile("data.db", {}, resolve, reject);
							},
							function(e) {
								reject(e);
							}
						);
					})
					.then((fileEntry) => {
						console.log("fileEntry path", fileEntry.fullPath);
						console.log("FileShare", FileShare.render);

						FileShare.render({
								type: "WX",
								filePath: fileEntry.fullPath,
							},
							(result) => {
								console.log("分享成功", result);
							}
						);
					})
					.catch((error) => {
						console.error("获取文件失败", error);
					});
			},

			async shareWithCopyFile2PDF() {
				let dataDBPath = "";
				uni.showLoading({
					title: "正在收集数据...",
				});

				try {
					dataDBPath = await new Promise((resolve, reject) => {
						plus.io.requestFileSystem(
							plus.io.PUBLIC_DOWNLOADS,
							function(fs) {
								fs.root.getFile("data.db", {}, resolve, reject);
							},
							function(e) {
								reject(e);
							}
						);
					});

					console.log(" dataDBPath", dataDBPath?.fullPath);
				} catch (error) {
					uni.hideLoading();
					throw new Error("获取文件失败");
				}

				if (!dataDBPath || !dataDBPath.fullPath) {
					uni.hideLoading();
					throw new Error("获取文件失败");
				}

				try {
					const dataPDFPath = await new Promise((resolve, reject) => {
						plus.io.requestFileSystem(
							plus.io.PUBLIC_DOWNLOADS,
							function(fs) {
								fs.root.getFile("data.pdf", {}, resolve, reject);
							},
							function(e) {
								reject(e);
							}
						);
					});

					console.log(" dataPDFPath", dataPDFPath?.fullPath);
					if (dataPDFPath && dataPDFPath?.fullPath) {
						await new Promise((resolve, reject) => {
							dataPDFPath.remove(resolve, reject);
						});
						console.log("pdf文件删除成功", 111112222);
					}
				} catch (error) {
					console.log("pdf文件不存在 不用删除", error);
				}

				const parentPath = await new Promise((resolve, reject) => {
					dataDBPath.getParent(resolve, reject);
				});
				console.log("parentPath", parentPath?.fullPath);

				try {
					const result = await new Promise((resolve, reject) => {
						dataDBPath.copyTo(parentPath, "data.pdf", resolve, reject);
					});
					console.log("result", result?.fullPath);

					setTimeout(() => {
						FileShare.render({
								type: "SYSTEM",
								filePath: result.fullPath,
							},
							(result) => {
								console.log("分享成功", result);
							}
						);
					}, 3000);
				} catch (error) {
					console.log("复制文件失败", error);
				} finally {
					setTimeout(() => {
						uni.hideLoading();
					}, 3000);
				}
			},

			createXlsx(data) {
				try {} catch (error) {
					console.log("创建Excel失败", error);
				}
			},

			toService() {
				uni.navigateTo({
					url: "/pages/ring/customerService",
				});
			},

			showUnbindRing() {
				if (!this.ringMac) return;
				uni.showModal({
					title: "解绑提示",
					content: "解绑戒指后，将会清空戒指数据，请慎重!",
					success(res) {
						if (res.confirm) {
							console.log("用户点击确定");
							try {
								unbindUserRing()
									.then(() => {
										if (store.state.bleManage.bluetoothStatus === BLUETOOTH_STATUS
											.CONNECTED) {
											store.dispatch("ringDataManage/factoryReset");
											store.dispatch("bleManage/disConnectDevice");
										}
										setTimeout(() => {
											plus.runtime.restart();
										}, 1000);
									})
									.catch((error) => {
										console.log("解绑报错", error);
									});
							} catch (error) {
								console.log("解绑 什么错误", error);
							}
						} else if (res.cancel) {
							console.log("用户点击取消");
						}
					},
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
	.ring_msg {
		padding: var(--status-bar-height) 30rpx;

		width: 100vw;
		min-height: 100vh;
		background-image: url("@/static/ring/ill/ill_bg.png");
		background-size: 100% 100%;
		background-repeat: repeat;
		background-position: center;

		.title {
			padding: 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 100%;

			view {
				flex: 1;
				text-align: center;
			}

			.left {
				font-size: 40rpx;
				font-weight: 500;
				color: #ffffff;
				padding: 22rpx 36rpx;
				align-self: flex-start;
				flex: 1;
				text-align: left;
			}

			.right {
				font-size: 32rpx;
				font-weight: 500;
				color: rgba(255, 255, 255, 0.3);
				padding-right: 36rpx;
				height: 1px;
			}
		}

		.card {
			margin-top: 24rpx;
			width: 100%;
			height: 334rpx;
			border-radius: 20rpx;
			background: rgba(255, 255, 255, 0.4);
			// background-color: #bdddf6;
			padding: 40rpx 40rpx;

			display: flex;
			justify-content: flex-start;
			align-items: center;

			.ring {
				height: 204rpx;
				margin-right: 40rpx;
			}

			.name {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.ring_name {
				font-weight: 500;
				font-size: 32rpx;
				text-align: center;
			}

			.status {
				border-radius: 10rpx;
				font-weight: 500;
				font-size: 20rpx;
				padding: 8rpx 10rpx;
				color: #07cf00;
				margin-left: 12rpx;

				&.link {
					background-color: rgba(7, 207, 0, 0.2);
				}

				&.unlink {
					background-color: rgba(7, 207, 0, 0.2);
				}

				&.linking {
					background-color: rgba(7, 207, 0, 0.2);
				}

				&::before {
					content: " ";
					width: 12rpx;
					height: 12rpx;
					background-color: #07cf00;
					border-radius: 50%;
					display: inline-block;
					margin-right: 5rpx;
				}
			}

			.battery {
				width: 320rpx;
				height: 28rpx;
				border-radius: 200rpx;
				background-color: rgba(0, 0, 0, 0.1);
				position: relative;
				margin: 20rpx 0;

				.progress {
					width: 100%;
					height: 100%;
					border-radius: 200rpx;
					background-color: #07cf00;
				}

				.value {
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);

					font-weight: 600;
					font-size: 20rpx;
					color: white;
				}
			}

			.addr_name,
			.addr_mac {
				font-weight: 600;
				font-size: 24rpx;
				color: #666666;
			}
		}

		.item {
			margin-top: 40rpx;

			h3 {
				margin-bottom: 12rpx;
			}

			.card {
				height: 100rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.label {
					font-weight: 500;
					font-size: 28rpx;
				}
			}

			.tips {
				margin-top: 8rpx;
				padding-left: 30rpx;
				color: #666666;
				font-weight: 400;
				font-size: 22rpx;
				line-height: 30rpx;
			}
		}
	}
</style>